package controllers;

import GUI.ServerUI;
import ocsf.server.*;


public class ServerUIP {

	private static final long serialVersionUID = 1L;
	private ServerUI UILayer; 
	private EchoServer eServer;
	private DBController db;
	public static IntSysClock clk;
	private JobsManager jobMngr;
	
	final public static int EXIT = 1;
	final public static int START = 2;
	final public static int STOP = 3;
	final public static int KeyRelease_DB_Port = 4;
	final public static int KeyRelease_DB_User = 5;
	final public static int KeyRelease_DB_Pass = 6;
	final public static int KeyRelease_DB_Name = 7;
	final public static int KeyRelease_DB_IP = 8;
	final public static int KeyRelease_Port = 9;
	final public static int KeyRelease_Limit = 10;
	
	
	
	
	
	
	public ServerUIP(ServerUIP suip){}

	public ServerUIP(ServerUI serUI) {
		// TODO Auto-generated constructor stub
	}

	public void exit() {
		// TODO Auto-generated method stub
		
	}
	
	
	
	
	
	public void start()
	{
		int res_conns;
		System.out.println("UIP@Server: START clicked/");
		UILayer.setStopEnable(true);
		UILayer.setStartEnable(false);
		UILayer.setExitEnable(false);
		// Check all fields to be filled out correctly.
		// ......
		
		UILayer.msgConsole("Starting service..");
		
		try
		{
			this.eServer = new EchoServer(UILayer.getPort(),this);
			String str_DB_Password = new String(UILayer.getDBPassword());
			this.db = new DBController( UILayer.getDBIP() , UILayer.getDBPort() 
					,UILayer.getDBUsername() , str_DB_Password , UILayer.getDBSchemeName());
			
			if ((res_conns=resetOldConnections()) > 0) 
				UILayer.msgConsole("UIP@Server: (start) " + res_conns + " connections has been restarted.");	
			eServer.listen(); //Start listening for connections
			if (!jobMngr.isRunning())
			{
				jobMngr.setDB(db);
				jobMngr.setClock(clk);
				jobMngr.setEcho(eServer);
				jobMngr.startCycle();
			}
			// Disable settings options
			UILayer.setSettings(false);
		}
		catch (IOException ioex) 
		{
			UILayer.setStopEnable(false);
			UILayer.setStartEnable(true);
			UILayer.setExitEnable(true);
			UILayer.msgConsole("ERROR - Could not start listening!");
		}
//		catch (InstantiationException | ClassNotFoundException | IllegalAccessException e) 
//		{
//			UILayer.setStopEnable(false);
//			UILayer.setStartEnable(true);
//			UILayer.setExitEnable(true);
//			UILayer.msgConsole("ERROR - JDBC Driver error!");			
//		}
		catch (SQLException e) 
		{
			UILayer.setStopEnable(false);
			UILayer.setStartEnable(true);
			UILayer.setExitEnable(true);
			UILayer.msgConsole("ERROR - Could not connect to DB using JDBC!");
		} 
		catch (InstantiationException e) 
		{
			UILayer.setStopEnable(false);
			UILayer.setStartEnable(true);
			UILayer.setExitEnable(true);
			UILayer.msgConsole("ERROR - JDBC Driver error!");
			e.printStackTrace();
		} 
		catch (IllegalAccessException e) 
		{
			UILayer.setStopEnable(false);
			UILayer.setStartEnable(true);
			UILayer.setExitEnable(true);
			UILayer.msgConsole("ERROR - JDBC Driver error!");
			e.printStackTrace();
		} 
		catch (ClassNotFoundException e) 
		{
			UILayer.setStopEnable(false);
			UILayer.setStartEnable(true);
			UILayer.setExitEnable(true);
			UILayer.msgConsole("ERROR - JDBC Driver error!");
			e.printStackTrace();
		}
	}
	
}
